package com.lotter.service;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.lotter.common.context.YcContext;
import com.lotter.common.util.DateUtil;
import com.lotter.context.GlobConst;
import com.lotter.dao.FianceQuartzMapper;
import com.lotter.dao.PresentCheckMapper;
import com.lotter.model.fianceplatorm.Fiancestatis;
import com.lotter.model.precentcheck.PresentVo;
import com.lotter.model.precentcheck.ProPresent;

/**
 * 财务统计业务处理service
 * @author panguixiang
 *
 */
@Service("fianceStatisService")
public class FianceStatisService {

	private static Logger logger = Logger.getLogger(FianceStatisService.class);
	
	@Autowired
	private FianceQuartzMapper fianceQuartzMapper;
	
	@Autowired
	private PresentCheckMapper presentCheckMapper;
	
	/**
	 * job
	 */
	public void fianceStatisWork(){
		String begintime = DateUtil.patternDateToStr(DateUtil.getNextDay(new Date()),"yyyy-mm-dd")+" 23:59:59";
		String endtime = DateUtil.patternDateToStr(new Date(),"yyyy-mm-dd")+" 23:59:59";
		Fiancestatis fiancestatics = new Fiancestatis();
		
		Date startDate;
		Date endDate;
		try {
			endDate = DateUtil.stringToDate(endtime,"yyyy-mm-dd HH:mm:ss");
			startDate = DateUtil.stringToDate(begintime,"yyyy-mm-dd HH:mm:ss");
			fiancestatics.setPlatformbalance(fianceQuartzMapper.getplatformbalance());
			//1=充值，2=购票，3=提现，4=平台转入，5=平台转出退票,6=房间提成
			fiancestatics.setBuyticflowmoney(fianceQuartzMapper.statisAccMoneySum(2, startDate, endDate));
			fiancestatics.setCreatedate(new Date());
			fiancestatics.setHoursemoney(fianceQuartzMapper.statisAccMoneySum(6, startDate, endDate));
			fiancestatics.setRechargeflowmoney(fianceQuartzMapper.statisAccMoneySum(1, startDate, endDate));
			fiancestatics.setWithdrawmoney(fianceQuartzMapper.statisAccMoneySum(3, startDate, endDate));
			fiancestatics.setRefundflowmoney(fianceQuartzMapper.statisAccMoneySum(5, startDate, endDate));//实际退票金额
			/**
			 * 实体店收入= 我们每天实际购票流水*1.5% （去掉退票的）
			 */
			long chargemoney = fiancestatics.getBuyticflowmoney()-fiancestatics.getRefundflowmoney();//一日，平台实际卖票收入
			fiancestatics.setShopflowmoney((long)((float)chargemoney*0.15));
			/**
			 * 平台赢亏=购票实际收入流水*8%-实体店收入（出票流水*4%）-房间分层-粉丝分成-充值手续费(充值*1%)
			 */
			long profit=(long) (
					(float)chargemoney*0.08-(float)fiancestatics.getShopflowmoney()-
					(float)fiancestatics.getHoursemoney()-(float)fiancestatics.getRechargeflowmoney()*0.01
					);
			fiancestatics.setPlatformprofit(profit);
		} catch (Exception e) {
			logger.error(e);
			e.printStackTrace();
		}
	}
	
	public Map<String,Object> finacelistPage(String startdatestr, String enddatestr,
			Integer page,Integer rows,Map<String,Object> result) throws Exception {
    	page=page==null?page=1:page;
		int start=(page-1)*rows;
		
		Date startdate=null,enddate=null;
		if(StringUtils.isNotBlank(startdatestr)) {
			startdate = DateUtil.stringToDate(startdatestr, YcContext.DATE_STR_PATTERN);
		}
		if(StringUtils.isNotBlank(enddatestr)) {
			enddate = DateUtil.stringToDate(enddatestr, YcContext.DATE_STR_PATTERN);
		}
		List<Fiancestatis> list = fianceQuartzMapper.fiancestatisListPage(startdate,enddate, start, rows);
		result.put(GlobConst.page_row,list);//rows键 存放每页记录 list  
		result.put(GlobConst.page_total, fianceQuartzMapper.fiancestatiscount(startdate,enddate));//total键 存放总记录数，必须的  
        return result;
    }
	/**
	 * 提现
	 * @param startdatestr
	 * @param enddatestr
	 * @param page
	 * @param rows
	 * @param result
	 * @return
	 * @throws Exception
	 */
	public Map<String,Object> presentChecklistPage(String startdatestr, String enddatestr,
			Integer page,Integer rows,Map<String,Object> result) throws Exception {
    	page=page==null?page=1:page;
		int start=(page-1)*rows;
		
		Date startdate=null,enddate=null;
		if(StringUtils.isNotBlank(startdatestr)) {
			startdate = DateUtil.stringToDate(startdatestr, YcContext.DATE_STR_PATTERN);
		}
		if(StringUtils.isNotBlank(enddatestr)) {
			enddate = DateUtil.stringToDate(enddatestr, YcContext.DATE_STR_PATTERN);
		}
		List<PresentVo> list = presentCheckMapper.getPresentVos(startdate,enddate, start, rows);
		result.put(GlobConst.page_row,list);//rows键 存放每页记录 list  
		result.put(GlobConst.page_total, presentCheckMapper.getPresentVoCount(startdate,enddate));//total键 存放总记录数，必须的  
        return result;
    }
	
	/**
	 * 执行 提现操作（修改数据表数据）
	 * @param ids
	 * @throws Exception
	 */
	public void presentDo(String ids) throws Exception{
		String[] idArrs=ids.split(",");
		ProPresent pro = new ProPresent();
		if(idArrs!=null && idArrs.length>0) {
			for(String id: idArrs) {
				if(StringUtils.isNotBlank(id)) {
					pro.setId(Long.parseLong(id));
					presentCheckMapper.proWorkZan(pro);
				}
			}
		}
	}
	
}
